#include <stdlib.h>
#include <stdio.h>
#include <spu_mfcio.h>
#include "../ubicus.h"
#include <mc_rand.h>
#include "./recu.h"



//#define DEBUG

/* allocating the control_block structure on the stack.  We align
 * it by  */
control_block_t control_block __attribute__ ((aligned (128)));




int main(unsigned long long speid __attribute__ ((unused)),
         unsigned long long argp, 
         unsigned long long envp __attribute__ ((unused)))
{
  unsigned int tag, id;
  int i, j;
  float a_min, a_max;
  float a, b;

  /* First, we reserve a MFC tag for use */
  tag = mfc_tag_reserve();
  if (tag == MFC_TAG_INVALID)
  {
    printf ("SPU ERROR, unable to reserve tag\n");
    return 1;
  }
  
/* DMA the control block information from system memory */
  
    mfc_get (&control_block, argp, sizeof (control_block_t), tag, 0, 0);

/*    wait for the DMA to complete */
    mfc_write_tag_mask (1 << tag);
    mfc_read_tag_status_all ();
    
/* fin de récupération des données du control_block  */    
    id = control_block.id;
    a_min = control_block.a_min;
    a_max = a_min + INTERVALLE_A;
    
    
/* création des vecteurs contenant des conditions initiales   */

mc_rand_ks_init(1234);

    
    vector float vec_float_array_x[NbVecsInit];
    vector float vec_float_array_y[NbVecsInit]; 

    
    
    for (a = a_min; a < a_max; a += PAS_A)
    {
    	vector float vec_a = {a, a, a, a};
    	for (b = B_MIN; b <= B_MAX; b += PAS_B)
    	{
    		vector float vec_b = {b, b, b, b};
    		mc_rand_ks_0_to_1_array_f4(NbVecsInit, vec_float_array_x);  
                mc_rand_ks_0_to_1_array_f4(NbVecsInit, vec_float_array_y);  
  		for (i = 0; i <= 1000; i++)
  		{
  			for (j = 0; j < NbVecsInit; j++)
  			{
			  recu(&(vec_float_array_x[j]),&(vec_float_array_y[j]), vec_a,  vec_b);	
  			}		
  		}
#ifdef DEBUG
		printf("a=%f,b=%f x=%f\n",(float)vec_a,float(vec_b),(float)vec_float_array_x[0]);  
#endif
	       
		
  	}
  }
  return 0;
}
